package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.assistant.AssistantSettingUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AssistantSettingUserDataDaoImpl extends TempDataObjectDao<AssistantSettingUserData> {

    public static String getAssistantSettingUserDataSql = "select * from `t_u_assistant_setting` where `userId`=?";

    public AssistantSettingUserData getAssistantSettingUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAssistantSettingUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateAssistantSettingUserDataSql = "update `t_u_assistant_setting` set operateTypeList=?,charitableTimes=?,beautyId=?,grandChildEnterSchoolMap=?,openComprehend=?,beautyFavorHundred=? where userId = ?";

    @Override
    protected boolean updateData(AssistantSettingUserData assistantSettingUserData) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(assistantSettingUserData.getOperateTypeList())));
        para.put(i++, new DbParameter(Types.INTEGER, assistantSettingUserData.getCharitableTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantSettingUserData.getBeautyId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(assistantSettingUserData.getGrandChildEnterSchoolMap())));
        para.put(i++, new DbParameter(Types.BOOLEAN, assistantSettingUserData.isOpenComprehend()));
        para.put(i++, new DbParameter(Types.BOOLEAN, assistantSettingUserData.isBeautyFavorHundred()));
        para.put(i++, new DbParameter(Types.BIGINT, assistantSettingUserData.getUserId()));
        if (getDb().executeNoneQuery(updateAssistantSettingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertAssistantSettingUserDataSql = "insert into `t_u_assistant_setting`(userId,operateTypeList,charitableTimes,beautyId,grandChildEnterSchoolMap,openComprehend,beautyFavorHundred) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(AssistantSettingUserData assistantSettingUserData) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, assistantSettingUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(assistantSettingUserData.getOperateTypeList())));
        para.put(i++, new DbParameter(Types.INTEGER, assistantSettingUserData.getCharitableTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantSettingUserData.getBeautyId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(assistantSettingUserData.getGrandChildEnterSchoolMap())));
        para.put(i++, new DbParameter(Types.BOOLEAN, assistantSettingUserData.isOpenComprehend()));
        para.put(i++, new DbParameter(Types.BOOLEAN, assistantSettingUserData.isBeautyFavorHundred()));
        if (getDb().executeNoneQuery(insertAssistantSettingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected AssistantSettingUserData parseData(ResultSet rs) throws SQLException {
        AssistantSettingUserData assistantSettingUserData = new AssistantSettingUserData();
        assistantSettingUserData.setUserId(rs.getLong("userId"));
        String operateTypeList = rs.getString("operateTypeList");
        if (!StringUtils.isNullOrEmpty(operateTypeList)) {
            assistantSettingUserData.setOperateTypeList(JSON.parseObject(operateTypeList, new TypeReference<List<Integer>>() {
            }));
        }
        assistantSettingUserData.setCharitableTimes(rs.getInt("charitableTimes"));
        assistantSettingUserData.setBeautyId(rs.getInt("beautyId"));
        String grandChildEnterSchoolMap = rs.getString("grandChildEnterSchoolMap");
        if (!StringUtils.isNullOrEmpty(grandChildEnterSchoolMap)) {
            assistantSettingUserData.setGrandChildEnterSchoolMap(JSON.parseObject(grandChildEnterSchoolMap, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
            }));
        }
        assistantSettingUserData.setOpenComprehend(rs.getBoolean("openComprehend"));
        assistantSettingUserData.setBeautyFavorHundred(rs.getBoolean("beautyFavorHundred"));
        return assistantSettingUserData;
    }


}
